def isP92Num(num):
    while num<>1 and num <>89:
        result=0
        for x in str(num):
            result=result+int(x)**2
        num=result
    if num==89:
        return True
    else:
        return False

n=10000000
result=0
while n>0:
    if isP92Num(n):
        result+=1
    if result%1000==0:
        print n,result
    n-=1
print result
